package com.wbs.shop.nuby.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.api.ApiHelper;
import com.wbs.shop.nuby.api.RxHelper;
import com.wbs.shop.nuby.bean.EmployeeBean;
import com.wbs.shop.nuby.helper.RefreshHelper;
import com.wbs.shop.nuby.utils.Utils;
import com.wbs.shop.nuby.view.activity.CashierActivityPadActivity;
import com.wbs.shop.nuby.view.adapter.SelectEmployeeAdapter;
import com.wbs.shop.nuby.view.base.BaseFragment;
import com.wbs.shop.nuby.widget.TitleView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectEmployeeFragment extends BaseFragment {
    private static final String IS_PAD = "IS_PAD";

    public static SelectEmployeeFragment newInstance(boolean isPad) {
        SelectEmployeeFragment selectEmployeeFragment = new SelectEmployeeFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_PAD, isPad);
        selectEmployeeFragment.setArguments(bundle);
        return selectEmployeeFragment;
    }

    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private RefreshHelper<List<EmployeeBean>> mRefreshHelper;
    private SelectEmployeeAdapter mSelectEmployeeAdapter;
    private boolean isPad = false;

    @Override
    protected int tellMeLayout() {
        return R.layout.fragment_select_employee;
    }

    @Override
    public void init(View rootView, Bundle savedInstanceState) {
        if (hasExtras()) {
            isPad = getArguments().getBoolean(IS_PAD, false);
            if (isPad) {
                titleView.setNavigationBackListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CashierActivityPadActivity) getActivity()).closeDrawers();
                    }
                });
            }
        }
        mSelectEmployeeAdapter = new SelectEmployeeAdapter(getContext());
        mSelectEmployeeAdapter.setOnClickAdapterListenter(this::onClicke);
        mRefreshHelper = new RefreshHelper<List<EmployeeBean>>(recyclerview, refreshLayout, mSelectEmployeeAdapter) {
            @Override
            public void loadData() {
                shopUserList();
            }
        };
        refreshLayout.setEnableLoadMore(false);
        recyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        ((DefaultItemAnimator) recyclerview.getItemAnimator()).setSupportsChangeAnimations(false);
        mRefreshHelper.onRefresh();
    }

    private void shopUserList() {
        ApiHelper.getApiService().shopUserList()
                .compose(RxHelper.handleResult(this))
                .subscribe(mRefreshHelper.getRxSubscribe());
    }

    public void onClicke(View view, int position) {
        EmployeeBean employeeBean = mSelectEmployeeAdapter.getItem(position);
        Intent data = new Intent();
        data.putExtra("employee", employeeBean);
        if (isPad) {
            ((CashierActivityPadActivity) getActivity()).closeDrawers();
            ((CashierActivityPadActivity) getActivity()).onActivityResultEmployee(data);
        } else {
            getActivity().setResult(Activity.RESULT_OK, data);
            getActivity().finish();
        }
    }
}
